package ltd.sumfsy.grain.service.impl;

import ltd.sumfsy.grain.bean.Data;
import ltd.sumfsy.grain.bean.example.DataExample;
import ltd.sumfsy.grain.mapper.DataMapper;
import ltd.sumfsy.grain.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 设备数据服务实现类
 * @author 范颂扬
 * @create 2021-05-13 10:55
 */
@Service
public class DataServiceImpl implements DataService {

    private final DataMapper dataMapper;

    @Autowired
    public DataServiceImpl(DataMapper dataMapper) {
        this.dataMapper = dataMapper;
    }

    @Override
    public boolean save(Data data) {
        int res = dataMapper.insertSelective(data);
        return res > 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        int res = dataMapper.updateByPrimaryKeySelective(new Data().setId(id).setDelFlag(1));
        return res > 0;
    }

    @Override
    public List<Data> queryAll() {
        return dataMapper.selectByExample(null);
    }

    @Override
    public List<Data> queryByTime(Date begin, Date end) {
        DataExample example = new DataExample();
        example.createCriteria().andDelFlagEqualTo(0)
                .andGetTimeBetween(begin, end);
        return dataMapper.selectByExample(example);
    }
}
